package servlet;

import entity.Lesson;
import service.LessonService;
import service.RatifyLessonService;
import service.impl.LessonServiceImpl;
import service.impl.RatifyLessonServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/lessonApplyServlet")
public class LessonApplyServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        String student = String.valueOf(session.getAttribute("student"));
        String lessoncode = String.valueOf(session.getAttribute("lessoncode"));
        String applyreason = request.getParameter("reason");
        LessonService service = new LessonServiceImpl();
        RatifyLessonService ratifyLessonService = new RatifyLessonServiceImpl();
        try {
            service.apply(lessoncode,student,applyreason);
            ratifyLessonService.apply(lessoncode,student,applyreason);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        response.sendRedirect("/lessonListServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}